package com.lh.tams.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lh.tams.domain.TamsModule;

/**
 * Session验证filter
 */
public class SessionFilter implements Filter {
	
	public void destroy() {

	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest httpRequest=(HttpServletRequest) request;
		HttpServletResponse httpResponse=(HttpServletResponse) response;
		String url = httpRequest.getRequestURI();
		
		if(url.endsWith(".adm")||url.matches(".*/pages/.*\\.jsp")){//Session验证拦截
			if(httpRequest.getSession() != null && httpRequest.getSession().getAttribute(Constants.ADMIN_SESSION_KEY) != null){//session不为空 
				//验证url是否有权限。。。
				List<String> limit=(List<String>) httpRequest.getSession().getAttribute(Constants.ADMIN_LIMIT);
				
				chain.doFilter(httpRequest, response);
				return;
			}else{//session为空
				if(url.contains("/login/login.adm")||url.contains("/login/logout.adm")||url.contains("/pages/login.jsp")){
					chain.doFilter(httpRequest, response);
					return;
				}else
					httpResponse.sendRedirect(httpRequest.getContextPath()+"/pages/login.jsp");
			}
		}
		chain.doFilter(httpRequest, httpResponse);

	}

	public void init(FilterConfig filterConfig) throws ServletException {
		
	}
}
